import { fromJS } from 'immutable';
import * as actionTypes from './actionTypes';

const defaultState = fromJS({
	topicList: [],
	articleList: [],
	recommendList: [],
	articlePage: 1,
	showScroll: false
});

const initHomeData = (state, action) => {
	return state.merge({
		topicList: fromJS(action.topicList),
		articleList: fromJS(action.articleList),
		recommendList: fromJS(action.recommendList)
	});
}

const addHomeData = (state, action) => {
	return state.merge({
		articleList: state.get('articleList').concat(action.list),
		articlePage: action.nextPage
	})
}

const reducer = (state = defaultState, action) => {
	switch(action.type) {
		case actionTypes.INIT_HOME_DATA:
			return initHomeData(state, action);
		case actionTypes.ADD_HOME_DATA:
			return addHomeData(state, action);
		case actionTypes.TOGGLE_SCROLL_TOP:
			return state.set('showScroll', action.show)
		default:
			return state;
	}
}

export default reducer;